<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorate="~{admin/layout}">

<head>
	<title>Admin Manage</title>
	<style>

	</style>
</head>

<body layout:fragment="content">
	<div class="card shadow">
		<div class="card-header border-bottom d-flex justify-content-between align-items-center">
			<h5 class="card-title mb-0">Admin List</h5>
			<div class="d-grid gap-2 d-md-block">
				<button class="btn btn-sm btn-primary" type="button" onclick="modifyAdminModal('')">New</button>
			</div>
		</div>
		<div class="card-body">
			<table id="adminTable"></table>
		</div>
		<!--<div class="card-footer pt-0">
			Footer
		</div>-->
	</div>
	<!---->
	<div class="modal fade" id="adminModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Admin Setting</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-md-12">
							<form id="adminForm">
								<input type="hidden" class="form-control" value="" id="adminId" name="adminId">
								<div class="mb-3 account-div">
									<label class="form-label">Account</label>
									<input type="text" class="form-control" value="" id="adminAccount" name="adminAccount">
								</div>
								<div class="mb-3">
									<label class="form-label">Full-Name</label>
									<input type="text" class="form-control" value="" id="adminName" name="adminName">
								</div>
								<div class="mb-3">
									<label class="form-label">Mobile</label>
									<input type="text" class="form-control" value="" id="adminMobile" name="adminMobile">
								</div>
								<div class="mb-3">
									<label class="form-label">Password</label>
									<input type="text" class="form-control" value="" id="adminPassword" name="adminPassword">
								</div>
								<!-- Save button -->
								<div class="d-flex justify-content-end mt-4">
									<button type="button" class="btn btn-default"
										onclick="closeAdminModal()">Close</button>
									<button type="submit" class="btn btn-primary">Submit</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal -->
	</div>
	<script layout:fragment="script">
		var adminFormValidate = null
		$(document).ready(function () {
			adminFormValidate = 
			$("#adminForm").validate({
				rules: {
					adminId: {
						required: false
					},
					adminAccount: {
						required: true
					},
					adminPassword: {
						required: false
					},
					adminName: {
						required: true
					},
					adminMobile: {
						required: true
					}
				},
				submitHandler: function (form) {
					var formData = $(form).serialize();
					if(!$('#adminId').val() && !$('#adminPassword').val()){
						layer.msg('Need Password');
						return false
					}
					saveAdmin(formData);
					return false
				}
			})
			loadAdminTable()
		});
		function loadAdminTable() {
			$('#adminTable').bootstrapTable('destroy');
			$('#adminTable').bootstrapTable({
				url: "[[@{'/admin/admins'}]]",
				uniqueId: 'adminId',
				dataType: "json",
				pagination: true,
				pageSize: 10,
				queryParams: function (params) {
					var temp = {
						size: params.limit,
						page: (params.offset / params.limit),
						sort: params.sort,
						sortOrder: params.order
					};
					return temp;
				},
				sidePagination: "server",
				pageList: [30, 50],
				columns: [{
					title: 'Sort',
					formatter: function (value, row, index) {
						var pageSize = $('#adminTable').bootstrapTable('getOptions').pageSize;
						var pageNumber = $('#adminTable').bootstrapTable('getOptions').pageNumber;
						return pageSize * (pageNumber - 1) + index + 1;
					}
				}, {
					field: 'adminAccount',
					title: 'Account',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					field: 'adminName',
					title: 'Full-Name',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					field: 'adminMobile',
					title: 'Mobile',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					field: 'adminTime',
					title: 'Created',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					title: 'Operate',
					field: 'adminId',
					class: 'text-center',
					width: '20%',
					formatter: function (value, row, index) {
						let res = '<button class="btn btn-sm btn-warning" onclick="modifyAdminModal(\'' + value + '\')">Modify</button>&nbsp;';
						res += '<button class="btn btn-sm btn-danger " onclick="removeAdmin(\'' + value + '\')">Remove</button>';
						return res;
					}
				},],
				onPostBody: function (value) {
					$(".fixed-table-container").css('padding-bottom', '37px');
				}
			});
		}
		function closeAdminModal() {
			adminFormValidate.resetForm()
			$('#adminModal').modal('hide')
		}
		function modifyAdminModal(adminId) {
			if (adminId) {
				var data = $("#adminTable").bootstrapTable('getRowByUniqueId', adminId);
				$('#adminId').val(data.adminId)
				$('#adminAccount').val(data.adminAccount)
				$('#adminPassword').val('')//data.adminPassword
				$('#adminName').val(data.adminName)
				$('#adminMobile').val(data.adminMobile)
				$('.account-div').hide();
			} else {
				$('#adminId').val('')
				$('#adminAccount').val('')
				$('#adminPassword').val('')
				$('#adminName').val('')
				$('#adminMobile').val('')
				$('.account-div').show();
			}
			$('#adminModal').modal('show')
		}
		function saveAdmin(data) {
			$.post(ctx + "/admin/saveAdmin", data, function (resp) {
				if (resp.status == 0) {
					loadAdminTable();
					$('#adminModal').modal('hide');
				}
				layer.msg(resp.msg);
			})
		}
		function removeAdmin(adminId) {
			layer.msg("Do you want to delete it", {
				title: 'tip', icon: 3, btn: ["Confirm", "Cancel"],
				yes: function () {
					$.ajax({
						type: "get",
						url: ctx + "/admin/delAdmin",
						data: {
							"adminId": adminId,
						},
						success: function (resp) {
							if (resp.status == 0) {
								loadAdminTable()
							}
							layer.msg(resp.msg);
						}
					});
				},
				btn2: function () {
					// layer.msg("Cancel");
				}
			});
		}
	</script>
</body>

</html>